
****************************************************************************************************************
*** PREDICTING VALUE OF OBSERVABLES:
* 		The following code has been modified in order to replicate the econometric specification 
* 		underpinning the original table by adding the gender interaction.
****************************************************************************************************************

***********************************************************
*STEP 1- CLEAN CONTROLS FOR LASSO
***********************************************************	
	local controls $controls
	*gen Winner_Male = Winner * Gender_Followup
	*gen Winner_Male_alt = Winner_Male
	
	*label var Winner_Male "Male Winner"
	*label var Winner_Male_alt "Male Winner"

	
	*PDS LASSO CODE

		cap drop _*
		cap drop *AA* 
		
		local k=1
		if "`controls'"!="" {
		foreach var of varlist `controls' {
			g AA`k'= `var'
				local ++k
			}
		}

		unab AA: AA*
			lassoClean `AA'
			ds _AA*
		
		foreach var of varlist _AA*{
			sum `var'
			gen miss_`var'=`var'==.
			replace `var'=r(mean) if miss_`var'==1
			gen Winner_`var'=Winner*`var'
			gen mi_Winner_`var'=Winner*miss_`var'
			}


***********************************************************
*STEP 2- RUN PDS LASSO FIR JUST CONTROLS
*grab the controls that pds lasso selects
***********************************************************		
	local m=1
	
	foreach out in  Trim_Profits_30Days log_Profits  Trim_Income log_Income {
		
	* run lasso
	pdslasso `out' Winner (Winner__AA* mi_Winner__AA* i.Surveyor_Code i.Survey_Version ///
		i.survey_month ) [weight=Propensity_Score] if  Survey_Version!=5 &  Done==1, ///
		partial(i.Surveyor_Code i.Survey_Version i.survey_month ) cluster(GroupNumber) fe
			
	*store selected variables
	global selected  `e(xselected_chs_l)'	
	
	gen MR_Prediction_Obs`m'=_b[Winner]

	foreach var of global selected {
		local variable=substr("`var'",8,20)
		display "`variable'"
		replace MR_Prediction_Obs`m'= MR_Prediction_Obs`m'+_b[`var']*`variable'
	}	

	tempvar rank_tercile
		egen `rank_tercile'=cut(MR_Prediction_Obs`m'), group(3)
		tab `rank_tercile', gen(MR_Prediction`m'_Obs_Tercile_)
		gen Winner_MR_Prediction`m'_Obs_T2= Winner * MR_Prediction`m'_Obs_Tercile_2
		gen Winner_MR_Prediction`m'_Obs_T3= Winner * MR_Prediction`m'_Obs_Tercile_3
		
		gen Male_W_MR_Prediction`m'_Obs_T2= Winner_Male * MR_Prediction`m'_Obs_Tercile_2
		gen Male_W_MR_Prediction`m'_Obs_T3= Winner_Male * MR_Prediction`m'_Obs_Tercile_3
	

***********************************************************
*STEP 3- NOW ADD RANK TO PREDICTION
*grab the controls that pds lasso selects
***********************************************************		
	
	* run lasso
	pdslasso `out'  Winner (Winner_Quint_Rank_NS Winner__AA* mi_Winner__AA*   i.Surveyor_Code i.Survey_Version ///
		i.survey_month ) [weight=Propensity_Score] if  Survey_Version!=5 &  Done==1, ///
		partial(i.Surveyor_Code i.Survey_Version i.survey_month ) cluster(GroupNumber) fe
		
		
	*store selected variables
	global selected  `e(xselected_chs_l)'	
	
	gen _Quint_Rank_NS`m'=Quint_Rank_NS
	gen MR_Prediction_Obs_Rank`m'=_b[Winner]
	
	local i=1 
	foreach var of global selected {
		local variable=substr("`var'",8,20)
		display "`variable'"
		replace MR_Prediction_Obs_Rank`m'= MR_Prediction_Obs_Rank`m'+_b[`var']*`variable'
	}	
	
	tempvar rank_tercile
		egen `rank_tercile'=cut(MR_Prediction_Obs_Rank`m'), group(3)
		tab `rank_tercile', gen(MR_Prediction`m'_Obs_Rank_T_)
		gen Winner_MR_Predict`m'_Obs_Rank_T2= Winner*MR_Prediction`m'_Obs_Rank_T_2
		gen Winner_MR_Predict`m'_Obs_Rank_T3= Winner*MR_Prediction`m'_Obs_Rank_T_3	
		
		gen Male_W_MR_Predict`m'_Obs_Rank_T2= Winner_Male * MR_Prediction`m'_Obs_Rank_T_2
		gen Male_W_MR_Predict`m'_Obs_Rank_T3= Winner_Male * MR_Prediction`m'_Obs_Rank_T_3	

	local m=`m'+1
	}

***********************************************************
*STEP 4- REGRESSION
***********************************************************				

	maketable_t4_2 Trim_Income log_Income Trim_Profits_30Days log_Profits if  Survey_Version!=5 &  Done==1 & Winner_Quint_Rank_NS!=. , /// 
	table("${outtex}${date}_MR_Predict_Obs_Rank_gender.tex") ///
	type(tex) /// 
	footnote($footnote_t4_gender) ///
	prehead1($prehead1) prehead2($prehead2) /// 
	posthead("\hline") ///
	title("Heterogeneity by Gender (based on Table 4 in Hussam et al. (2022b))") label(predictobservablesrank_gender)
	
